package com.business_clean.ui.adapter.address;

import android.text.TextUtils;
import android.view.View;

import com.business_clean.R;
import com.business_clean.data.mode.address.AddressListEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.ext.LogExtKt;

public class AddressMangerAdapter extends BaseQuickAdapter<AddressListEntity, BaseViewHolder> {

    private int isMultiple = 0;// 是否选中的状态

    public AddressMangerAdapter(int isSelected) {
        super(R.layout.item_address_manager);
        this.isMultiple = isSelected;
        addChildClickViewIds(R.id.iv_item_address_del);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, AddressListEntity item) {
        holder.setText(R.id.tv_item_address_title, item.getAddress())
                .setText(R.id.tv_item_address_desc, item.getAddress_desc());
        if (isMultiple == 1 || isMultiple == 2) {
            holder.getView(R.id.iv_item_address_choose).setVisibility(item.isChoice() ? View.VISIBLE : View.GONE);
            holder.getView(R.id.iv_item_address_del).setVisibility(View.GONE);
        } else {
            holder.getView(R.id.iv_item_address_del).setVisibility(View.VISIBLE);
            holder.getView(R.id.iv_item_address_choose).setVisibility(View.GONE);
        }
    }


    /**
     * 看是否需要单选
     */
    public void updateItem(int position) {
        if (getData().get(position).isChoice()) {
            getData().get(position).setChoice(false);
        } else {
            getData().get(position).setChoice(true);
        }
        notifyDataSetChanged();
    }

    /**
     * 反选状态
     *
     * @param uuids
     */
    public void dataInvert(String uuids) {
        if (!TextUtils.isEmpty(uuids)) {
            String[] dataArray = uuids.replaceAll("\n", "").split(",");
            for (String data : dataArray) {
                LogExtKt.logE("地址的数组->" + data, "");
                for (AddressListEntity addressList : getData()) {
                    if (addressList.getUuid().equals(data)) {
                        addressList.setChoice(true);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 获取已经反选的地址
     */
    public List<AddressListEntity> getChoiceAddressData() {
        List<AddressListEntity> choices = new ArrayList<>();
        for (AddressListEntity item : getData()) {
            if (item.isChoice()) {
                choices.add(item);
            }
        }
        return choices;
    }
}